﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_4
        Inherits EIBDataEIS_Base

        Protected _day As Byte = Nothing
        Protected _month As Byte = Nothing
        Protected _year As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _day = data(1) And &H1F
            _month = data(2) And &HF
            _year = data(3) And &H7F

            If _day > 31 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If _month > 12 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If _year > 99 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
        End Sub

        Sub New(day As Byte, month As Byte, year As Byte)
            If day > 31 Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If month > 12 Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If year > 99 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _day = day
            _month = month
            _year = year
            MyBase._data = New Byte() {0, (_day And &H1F), (_month And &HF), (_year And &H7F)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("{0}.{1}.{2}", _day, _month, _year)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_day, _month, _year}
        End Function

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("Day", _day))
            list.Add(New EIB.Info("Month", _month))
            list.Add(New EIB.Info("Year", _year))

            Return list
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Byte), GetType(Byte), GetType(Byte)}
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 4
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 4"
            End Get
        End Property


        Public Shared Function FromDateTime(value As DateTime) As EIBDataEIS_4
            Return New EIBDataEIS_4(value.Day, value.Month, value.Year)
        End Function

        Public ReadOnly Property ToDateTime As DateTime
            Get
                Return New DateTime(_year, _month, _day)
            End Get
        End Property

        Public ReadOnly Property Day As Byte
            Get
                Return _day
            End Get
        End Property

        Public ReadOnly Property Month As Byte
            Get
                Return _month
            End Get
        End Property

        Public ReadOnly Property Year As Byte
            Get
                Return _year
            End Get
        End Property

        Public ReadOnly Property FullYear As Short
            Get
                Return If(_year < 90, 2000 + _year, 1900 + _year)
            End Get
        End Property
    End Class
End Namespace